import { ApplicationRef, ComponentFactoryResolver, Injectable, Injector } from '@angular/core';
import { ZBPopupComponent } from './zbpopup.component';

@Injectable()
export class ZbpopupService {

  constructor(private injector: Injector,
              private application: ApplicationRef,
              private componentFactor: ComponentFactoryResolver) {}
  // 新增弹框
  showPopupAlertMessage(message: string) {

      const element = document.createElement('popup-component');

      const factory = this.componentFactor.resolveComponentFactory(ZBPopupComponent);

      const popupComponentRef = factory.create(this.injector, [], element);

      this.application.attachView(popupComponentRef.hostView);

      // 监听关闭事件
      popupComponentRef.instance.closed.subscribe(()=>{
        document.body.removeChild(element);
        this.application.detachView(popupComponentRef.hostView);
      });

      popupComponentRef.instance.message = message;

      // 增加控件到DOM
      document.body.appendChild(element);
  }

}
